package com.saturn.titan.web.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saturn.titan.core.bean.BusiDeployBill;
import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.bean.PlatformEmp;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.comm.ResponseBean;
import com.saturn.titan.core.dto.WorkBillDto;
import com.saturn.titan.core.service.AreaService;
import com.saturn.titan.core.service.DeployBillService;
import com.saturn.titan.core.service.EmpService;
import com.saturn.titan.core.service.WorkBillService;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/workBill")
public class WorkBillController extends BaseController{

	@Autowired
	private WorkBillService workBillService;
	
	@Autowired
	private AreaService areaService;
	
	@Autowired
	private EmpService empService;
	
	@Autowired
	private DeployBillService deployBillService;
	
	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String index(@RequestParam(value="phone",required=false) String phone,Model model) throws ParseException {
		model.addAttribute("phone",phone==null?"":phone);
		return "workBill/newWorkBill";
	}
	
	@RequestMapping(value = "select", method = RequestMethod.GET)
	public String select(Model model) throws ParseException {
		return "workBill/selectWorkBill";
	}
	
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public String insert(@Valid BusiWorkBill workBill,@RequestParam(value="type") String type,Model modle) throws ParseException {
		
		workBillService.insert(workBill);
		
		System.out.println(workBill.getId());
		
		if(type.equals("saveAdd")){
			return "workBill/newWorkBill";
		}else if(type.equals("save")){
			modle.addAttribute("workbill", workBill);
			return "workBill/viewWorkBill";
		}
		return null;
	}
	
	@RequestMapping(value = "viewBillById", method = RequestMethod.POST)
	public String viewBillById(@RequestParam(value="id") Long id,Model model) throws ParseException {
		model.addAttribute("workbill", workBillService.findById(id));
		
		Long userid = deployBillService.findCurrentEmpByWorkBillId(id);
		model.addAttribute("currentEmpId", userid);
		model.addAttribute("currentEmp", CRMCache.displayOpr(userid));
		model.addAttribute("currentAccount", CRMCache.displayAccount(userid));
		return "workBill/viewWorkBill";
	}
	
	@RequestMapping(value = "selectWorkBill")
	@ResponseBody
	public  Map<String, Object> selectWorkBill(@RequestParam(value="customerPhone") String customerPhone,
			@RequestParam(value="billStt") Integer billStt ,
			@RequestParam(value="requestType") Integer requestType,
			@RequestParam(value="emergentType") Integer emergentType,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows
			) throws ParseException {
		BusiWorkBill bill = new BusiWorkBill();
		if(null!=customerPhone && customerPhone.length()>0){
			bill.setCustomerPhone(customerPhone);
		}
		if(null!=billStt){
			bill.setBillStt(billStt);
		}
		if(null!=requestType){
			bill.setRequestType(requestType);
		}
		if(null!=emergentType){
			bill.setEmergentType(emergentType);
		}
		List<BusiWorkBill> list = workBillService.getAllWorkBill(bill);
		
		List<BusiWorkBill> returnList = new ArrayList<BusiWorkBill>();
		
		int size = list.size();
		int beginIndex = (page-1) * rows;
		int endIndex = page*rows-1;
		
		if(endIndex > size-1){
			endIndex = size-1;
		}
		for(int i=beginIndex;i<=endIndex;i++){
			returnList.add(list.get(i));
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total",list.size());//total键 存放总记录数，必须的  
        jsonMap.put("rows", returnList);//rows键 存放每页记录 list  
        
        
		return jsonMap;
	}
	
	
	@RequestMapping(value = "selectWorkBillInfo")
	@ResponseBody
	public  Map<String, Object> selectWorkBillInfo(@RequestParam(value="customerPhone") String customerPhone,
			@RequestParam(value="billStt") String billStt ,
			@RequestParam(value="requestType") String requestType,
			@RequestParam(value="emergentType") String emergentType,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows
			) throws ParseException {
		
		List<WorkBillDto> list = workBillService.getAllWorkBillInfo(customerPhone,billStt,requestType,emergentType);
		
		List<WorkBillDto> returnList = new ArrayList<WorkBillDto>();
		
		int size = list.size();
		int beginIndex = (page-1) * rows;
		int endIndex = page*rows-1;
		
		if(endIndex > size-1){
			endIndex = size-1;
		}
		for(int i=beginIndex;i<=endIndex;i++){
			returnList.add(list.get(i));
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total",list.size());//total键 存放总记录数，必须的  
        jsonMap.put("rows", returnList);//rows键 存放每页记录 list  
        
        
		return jsonMap;
	}
	
	@RequestMapping(value = "workBillVisit")
	@ResponseBody
	public  Map<String, Object> workBillVisit(@RequestParam(value="id") String id,@RequestParam(value="billStt") String billStt) throws ParseException {
		BusiWorkBill bill = workBillService.findById(Long.parseLong(id));
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		if(null!=bill){
			bill.setBillStt(Integer.parseInt(billStt));
			int re = workBillService.update(bill);
			if(re==1){
				  jsonMap.put("result","OK");
			}else{
				 jsonMap.put("result","fail");
			}
			
		}
		
        
		return jsonMap;
	}
	
	
	@RequestMapping(value = "selectAreaInfo")
	@ResponseBody
	public  Map<String, Object> selectAreaInfo() throws ParseException {
	
		//获取区域信息
		List list = areaService.findAllArea();
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		
		
        jsonMap.put("rows", list);
        
		return jsonMap;
	}
	
	@RequestMapping(value = "selectEmpInfo")
	@ResponseBody
	public  Map<String, Object> selectEmpInfo(@RequestParam(value="areaId") Long areaId) throws ParseException {
	
		List list = empService.getEmpByAreaAndRole(areaId);
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		
	
        jsonMap.put("rows", list);
        
		return jsonMap;
	}
	@RequestMapping(value = "workBillAreaAssign")
	@ResponseBody
	public  Map<String, Object> workBillAreaAssign (@RequestParam(value="billId") String billId,@RequestParam(value="roleId") String roleId){
		Long empId = Long.parseLong(roleId);
		Long workBillId = Long.parseLong(billId);
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		try{
			deployBillService.AreaDeployAssign(empId, workBillId);
			 jsonMap.put("result","OK");
		}catch(Exception e){
			 jsonMap.put("result","FAIL");
		}
		 return jsonMap;
	}
	
	@RequestMapping(value = "getManagedEmp")
	@ResponseBody
	public Map<String,Object> getManagedEmp(@RequestParam(value="parentId") String parentId){
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		Long superEmpId = Long.parseLong(parentId);
		PlatformEmp ep = new PlatformEmp();
		ep.setSuperEmpId(superEmpId);
		List list = empService.findEmp(ep);
		jsonMap.put("rows", list);
		return jsonMap;
	}
	
	
	@RequestMapping(value = "managedEmpAssign")
	@ResponseBody
	public Map<String,Object> managedEmpAssign(
			@RequestParam(value="deployId") String deployId,
			@RequestParam(value="empId") String empId,
			@RequestParam(value="workBillId") String workBillId){
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		Long assignId  = Long.parseLong(deployId);
		Long emp =  Long.parseLong(empId);
		Long id = Long.parseLong(workBillId);
		BusiWorkBill busiWorkBill = workBillService.findById(id);
		
		BusiDeployBill busiDeployBill = deployBillService.findById(assignId);
		if(null!=busiDeployBill && null!=busiWorkBill){
			busiDeployBill.setEmpId(emp);
			
			try{
				deployBillService.updateAssignEmp(busiDeployBill);
				 jsonMap.put("result","OK");
			}catch(Exception e){
				 jsonMap.put("result","FAIL");
			}
		}
		return jsonMap;
	}
	
	
	@RequestMapping(value = "managedEmpAssignByView")
	@ResponseBody
	public Map<String,Object> managedEmpAssign(
			@RequestParam(value="empId") String empId,
			@RequestParam(value="workBillId") String workBillId){
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		
		Long emp =  Long.parseLong(empId);
		Long id = Long.parseLong(workBillId);
		
		Long assignId  = deployBillService.findCurrentDeployIdByWorkBillId(id);
		BusiWorkBill busiWorkBill = workBillService.findById(id);
		
		BusiDeployBill busiDeployBill = deployBillService.findById(assignId);
		if(null!=busiDeployBill && null!=busiWorkBill){
			busiDeployBill.setEmpId(emp);
			
			busiWorkBill.setBillStt(1);
//			busiWorkBill.setAssignBillId(emp);
			try{
				workBillService.update(busiWorkBill);
				deployBillService.updateAssignEmp(busiDeployBill);
				 jsonMap.put("result","OK");
			}catch(Exception e){
				 jsonMap.put("result","FAIL");
			}
		}
		return jsonMap;
	}
	
	@RequestMapping(value = "cancleWorkBill")
	@ResponseBody
	public ResponseBean cancleWorkBill(@RequestParam(value="id") Long id){
		ResponseBean b = new ResponseBean();
		b.setResult(ResponseBean.RESULT_OK);
		workBillService.cancle(id);
		return b;
	}
}
